package com.envision.mdm.service.impl;

import com.envision.eeop.api.EnvisionClient;
import com.envision.eeop.api.EnvisionDefaultClient;
import com.envision.eeop.api.domain.DomainPoint;
import com.envision.eeop.api.domain.MdmChildObjects;
import com.envision.eeop.api.domain.MdmObjectAttributes;
import com.envision.eeop.api.request.*;
import com.envision.eeop.api.response.*;
import com.envision.mdm.service.MdmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <strong>Title : MdmServiceImpl.java<br>
 * </strong> <strong>Description : </strong><br>
 * <strong>Create on : 2018年07月10日<br>
 * <p>
 *
 * @author karrychang<br>
 * @version <strong>1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人		修改日期		修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("mdmServiceImpl")
public class MdmServiceImpl implements MdmService {
    private static final Logger  logger = LoggerFactory.getLogger(MdmServiceImpl.class);

    private static final String USER = "yongbing.zhang";
    private static final String PASSWORD = "1234pass5678";
    private final static String APP_KEY = "f277e6f5-6eb2-4b10-a9ac-2320c4b3f29e";
    private final static String APP_SECRET = "D0D6E093D01AFEF93339D8B41F6E6B50";
    private final static String GATEWAY = "https://eosaz.envisioncn.com/eeop";

    private static EnvisionClient client = new EnvisionDefaultClient(GATEWAY, APP_KEY, APP_SECRET);
    private  String token;
    private  String refreshToken;

    /** 
    *************************************************************************
     * MdmServiceImpl.java——EopToken<br>
     * Author: karrychang<br>
     * Date: 2018/7/10<br>
     * Description:获取token<br>
     * Used in：<br>
     * @param 
     * @return
     *************************************************************************
     */
    public String EopToken(){
        UserLoginRequest loginRequest = new UserLoginRequest();
        try {
            loginRequest.setPassword(PASSWORD);
            loginRequest.setName(USER);
            UserLoginResponse loginResponse = client.execute(loginRequest);
            token = loginResponse.getAccessToken();
            System.out.println(token);
            refreshToken = loginResponse.getRefreshToken();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("================获取token异常================");
        }
        return token;
    }
    /** 
    *************************************************************************
     * MdmServiceImpl.java——getObjectAttributes<br>
     * Author: karrychang<br>
     * Date: 2018/7/10<br>
     * Description:获取对象属性列表数据<br>
     * Used in：<br>
     * @param mdmIDList
     * @param attributeList
     * @return
     *************************************************************************
     */
    public  Map<String, MdmObjectAttributes> getObjectAttributes(List<String> mdmIDList , List<String> attributeList){
        try {
            MdmObjectAttributesGetRequest request = new MdmObjectAttributesGetRequest(mdmIDList, attributeList);
            EopToken();
            MdmObjectAttributesGetResponse response = client.execute(request, token);
            return response.getMdmObjects();
        }catch (Exception e){
            logger.error("query getObjectAttributes mdmids ({}) ,attr ({}) ,mesage ({})",mdmIDList,attributeList,e.getMessage());
        }
        return new HashMap<>();
    }
    /** 
    *************************************************************************
     * MdmServiceImpl.java——getmdmidspoints<br>
     * Author: karrychang<br>
     * Date: 2018/7/10<br>
     * Description:获取实时数据<br>
     * Used in：<br>
     * @param deviceIDList
     * @param pointIDList
     * @param fieldList
     * @return
     *************************************************************************
     */
    public Map<String, DomainPoint> getmdmidspoints(List<String> deviceIDList , List<String> pointIDList, List<String> fieldList){
        try {
            MdmDomainPointsGetRequest request = new MdmDomainPointsGetRequest(deviceIDList, pointIDList, fieldList);
            EopToken();
            MdmDomainPointsGetResponse response = client.execute(request, token);
            return response.getMdmMap();
        }catch (Exception e){
            logger.error("query getmdmidspoints deviceIDList ({}) , pointIDList({}),fieldList ({}),mesage ({})",deviceIDList,pointIDList,fieldList,e.getMessage());
        }
        return new HashMap<String, DomainPoint>();
    }
    /** 
    *************************************************************************
     * MdmServiceImpl.java——getDetailsV2<br>
     * Author: karrychang<br>
     * Date: 2018/7/10<br>
     * Description:获取历史明细数据V2<br>
     * Used in：<br>
     * @param mdmIds
     * @param metrics
     * @param beginTime
     * @param endTime
     * @return
     *************************************************************************
     */
    public List<Map<String,String>> getDetailsV2(List<String> mdmIds ,List<String> metrics,String beginTime,String endTime){
        try {
            DomainDetailsGetRequestV2 request = new DomainDetailsGetRequestV2(mdmIds, metrics, beginTime, endTime);
            EopToken();
            DomainMetricsGetResponse response = client.execute(request, token);
            return response.getDomainMetricList();
        }catch (Exception e){
            logger.error("query getDetailsV2 mdmIds ({}) , metrics({}),beginTime ({}),endTime ({}), mesage ({})",mdmIds,metrics,beginTime,endTime,e.getMessage());
        }
        return null;
    }
    /** 
    *************************************************************************
     * MdmServiceImpl.java——getObject<br>
     * Author: karrychang<br>
     * Date: 2018/7/10<br>
     * Description:获取对象列表数据<br>
     * Used in：<br>
     * @param mdmIDList
     * @param typeList
     * @param attributeList
     * @return
     *************************************************************************
     */
    public Map<String, MdmChildObjects> getObject(List<String> mdmIDList ,List<String> typeList,List<String> attributeList){
        try {
            MdmObjectsGetRequest request = new MdmObjectsGetRequest(mdmIDList, typeList, attributeList);
            EopToken();
            MdmObjectsGetResponse response = client.execute(request, token);
            return response.getMdmChildObjects();
        }catch (Exception e){
            logger.error("query getObject mdmIDList ({}) , typeList({}),attributeList ({}), mesage ({})",mdmIDList,typeList,attributeList,e.getMessage());
        }
        return null;
    }

    /** 
    *************************************************************************
     * MdmServiceImpl.java——getdayvalues<br>
     * Author: karrychang<br>
     * Date: 2018/7/10<br>
     * Description:获取零点值<br>
     * Used in：<br>
     * @param deviceIDList
     * @param pointIDList
     * @return
     *************************************************************************
     */
    public Map<String, DomainPoint> getdayvalues(List<String> deviceIDList,List<String> pointIDList){
        try {
            MdmDomainPointsDayValueGetRequest request = new MdmDomainPointsDayValueGetRequest(deviceIDList, pointIDList);
            EopToken();
            MdmDomainPointsGetResponse response = client.execute(request, token);
            return response.getMdmMap();
        }catch (Exception e){
            logger.error("query getdayvalues deviceIDList ({}) , pointIDList({}), mesage ({})",deviceIDList,pointIDList,e.getMessage());
        }
        return null;
    }


}
